package com.fauks.ee.mq.rabbit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建并配置连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.50.231");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("Sun830615");

        //通过连接工厂获取连接
        Connection connection = connectionFactory.newConnection();

        //通过连接获取Channel
        Channel channel = connection.createChannel();

        //通过channel发送数据
        for (int i = 0; i < 5; i++) {
            channel.basicPublish("", "test001", null, "Hello RabbitMQ".getBytes());
        }

        //关闭资源
        channel.close();
        connection.close();
    }
}
